extends Control

@onready var menu_bg_canvas_layer: CanvasLayer = $MenuBgCanvasLayer
@onready var menu_bg_texture_rect: TextureRect = $MenuBgCanvasLayer/MenuBgTextureRect
@onready var menu_bg_animation_player: AnimationPlayer = $MenuBgCanvasLayer/MenuBgAnimationPlayer
@onready var card_choose: Control = $MenuBgCanvasLayer/CardChoose
@onready var h_card_box: HBoxContainer = $MenuBgCanvasLayer/CardChoose/V/HCardBox
@onready var pause_screen: Control = $MenuBgCanvasLayer/PauseScreen
@onready var card_inventory: CardInventory = $MenuBgCanvasLayer/CardInventory

var is_show: bool = false
var is_keyboard_pause: bool = false
var is_level_up_pause: bool = false
var is_check_bag_pause: bool = false

func _ready() -> void:
	hide_menu()
	card_choose.hide()
	pause_screen.hide()
	card_inventory.hide()

func _input(event: InputEvent) -> void:
	if event.is_action_pressed("call_menu"):
		if not is_show:
			is_keyboard_pause = true
			Game.stop_game()
			open_menu()
		elif is_keyboard_pause:
			is_keyboard_pause = false
			Game.continue_game()
			close_menu()
	
	if event.is_action_pressed("open_inventory"):
		if not is_show:
			is_check_bag_pause = true
			Game.stop_game()
			open_menu()
		elif is_check_bag_pause:
			is_check_bag_pause = false
			card_inventory.close()
			Game.continue_game()
			close_menu()

func close_menu() -> void:
	is_show = false
	menu_bg_texture_rect.visible = false
	menu_bg_animation_player.play("menu_quit")

func open_menu() -> void:
	is_show = true
	menu_bg_canvas_layer.visible = true
	menu_bg_animation_player.play("menu_enter")

func hide_menu() -> void:
	menu_bg_texture_rect.visible = false
	menu_bg_canvas_layer.visible = false

func show_menu() -> void:
	menu_bg_texture_rect.visible = false
	if is_level_up_pause:
		card_choose.show()
	if is_keyboard_pause:
		pause_screen.show()
	if is_check_bag_pause:
		card_inventory.open(Game.inventory)

func _on_skip_pressed() -> void:
	is_level_up_pause = false
	card_choose.hide()
	Game.continue_game()
	close_menu()

func _on_pause_screen_continue_button_press() -> void:
	is_keyboard_pause = false
	pause_screen.hide()
	Game.continue_game()
	close_menu()
